#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileInfo>
#include <QDir>
#include <QPalette>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

   showFullScreen();



   checkFile("data","config.ini");

   prameter_init();

   UI_init();


   socket1 = new QTcpSocket(this);

   socket1->connectToHost(config->value("network/ip").toString(),8080);
   if (socket1->waitForConnected(1000))
   {
       qDebug() << "连接成功!";
   }
   else
   {
       QMessageBox::warning(this, tr("提示"), tr("连接服务器失败!,请修改正确ip后重启应用"), QMessageBox::Ok);
   }

   connect(socket1,&QTcpSocket::disconnected,[=](){
       QMessageBox::warning(this, tr("提示"), tr("服务器断开连接"), QMessageBox::Ok);

   });

}

MainWindow::~MainWindow()
{

}

void MainWindow::checkFile(const QString &directory, const QString &filename)
{



    // 判断目录是否存在，不存在则创建
        QDir dir(directory);
        if (!dir.exists()) {
            dir.mkpath(directory);
             QMessageBox::warning(this, "提示",directory);
        }

        // 判断文件是否存在，不存在则创建
        QString filePath = directory + "/" + filename;
        QFile file(filePath);
        if (!file.exists()) {
              QMessageBox::warning(this, "提示","创建配置文件");
            file.setFileName(filePath);
            if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {

            }
            file.close();


            config=new  QSettings(filePath, QSettings::IniFormat);

            //初始化参数
            config->setValue("prameter/button_W",height()/12*2);
            config->setValue("prameter/button_H",height()/12);
            config->setValue("prameter/button_num",17);
            config->setValue("prameter/Top_margin",height()/12);
            config->setValue("prameter/Left_margin",height()/12);
            config->setValue("prameter/up_down_Spacing",width()/12);
            config->setValue("prameter/Left_right_spacing",width()/12);
            config->setValue("network/ip","127.0.0.1");

            for (int var = 0; var < 17; ++var) {

                  config->setValue(QString("button%1/name").arg(var),"ss");
            }
        }else
        {

             config=new  QSettings(filePath, QSettings::IniFormat);
        }





}

void MainWindow::prameter_init()
{


    button_W=config->value("prameter/button_W").toUInt();
    button_H=config->value("prameter/button_H").toUInt();
    button_num=config->value("prameter/button_num").toUInt();
    Top_margin=config->value("prameter/Top_margin").toUInt();
    Left_margin=config->value("prameter/Left_margin").toUInt();
    up_down_Spacing=config->value("prameter/up_down_Spacing").toUInt();
    Left_right_spacing=config->value("prameter/Left_right_spacing").toUInt();
    button_nums=button_num;
}

void MainWindow::UI_init()
{


    StackedWidget = new QStackedWidget(this);
    StackedWidget->resize(this->width(), this->height());
    StackedWidget->show();



    //初始界面
    init_page = new QWidget(StackedWidget);
    init_page->resize(this->width(), this->height());
    init_page->setStyleSheet("border-image: url(:/data/beiji.png);");
    StackedWidget->addWidget(init_page);

    init_page->show();

    //配置界面
    config_page = new QWidget(StackedWidget);
    config_page->resize(this->width(), this->height());
    config_page->setStyleSheet("border-image: url(:/data/beiji.png);");
    StackedWidget->addWidget(config_page);
    config_page->show();

    //切换界面按钮
    QPushButton *handoff_button=new QPushButton(this);
    handoff_button->resize(button_W,button_H);
    handoff_button->move(init_page->width()-button_W-button_H/2,button_H/2);
    handoff_button->setText("设置");
    Buttoninit(handoff_button,"but_1","but_2");






    connect(handoff_button,&QPushButton::clicked,[=](){



        if(StackedWidget->currentIndex()==0)
        {
             handoff_button->setText("返回");
             StackedWidget->setCurrentIndex(1);
              handoff_button->show();
        }else
        {
             handoff_button->setText("设置");
             StackedWidget->setCurrentIndex(0);
              handoff_button->show();
        }


        hid_num++;
        if(hid_num==10)
        {
             hid_num=0;
             handoff_button->hide();
        }



    });



    for (int var = 0; var <18; ++var) {

        QPushButton *but=new QPushButton(init_page);

        but->resize(button_W,button_H);

        but->setText(config->value(QString("button%1/name").arg(var)).toString());

        but->move(Left_margin+(button_W+button_H)*int(var/5),Top_margin+(button_H*(var%5)*2)-1);
        Buttoninit(but,"but_1","but_2");
        But_vecors.push_back(but);
        if(var<button_num)
        {
            but->show();
        }
        connect(but,&QPushButton::clicked,[=](){

            if(var==0)
            {
             show_num++;
              if(show_num==10)
              {
                 show_num=0;
                  handoff_button->show();
              }
            }
            else
            {

              show_num=0;
            }

            if (socket1->waitForConnected(10))
            {
                QString cmd;
                if(button_nums-1==var)
                {
                    cmd = QString("%1").arg(17);
                    socket1->write(cmd.toLatin1());

                }else
                {
                    cmd = QString("%1").arg(var+1);
                    socket1->write(cmd.toLatin1());
                }
            }
            else
            {
                QMessageBox::warning(this, tr("提示"), tr("未连接服务器"), QMessageBox::Ok);
            }



        });

    }





    for (int var = 0; var <18; ++var) {

        QLineEdit *Line=new QLineEdit(config_page);

        Line->setAlignment(Qt::AlignCenter);
        Line->resize(button_W,button_H);
        Line->setStyleSheet("border-image:url(:data/inpt.png);color:red;font-size:14px;");
        Line->setText(config->value(QString("button%1/name").arg(var)).toString());

        Line->move(Left_margin+(button_W+button_H)*int(var/5),Top_margin+(button_H*(var%5)*2)-1);

        if(var<button_num)
        {
             Line->show();
        }

        Line_vecors.push_back(Line);
        connect(Line,&QLineEdit::textChanged,[=](QString str){


           if(str!="结束")
             {
               But_vecors[var]->setText(str);

               config->setValue(QString("button%1/name").arg(var),str);
             }

        });

    }

    But_vecors[button_num-1]->setText("结束");
    Line_vecors[button_num-1]->setText("结束");


    //ip输入
    QLineEdit *Line_ip=new QLineEdit(config_page);
    Line_ip->setText(config->value(QString("network/ip")).toString());
    Line_ip->resize(button_W,button_H);
    Line_ip->move(init_page->width()-button_W-button_H/2,button_H*3);
    Line_ip->setAlignment(Qt::AlignCenter);
    Line_ip->setStyleSheet("border-image:url(:data/inpt.png);color:red;font-size:14px;");
    Line_ip->show();

    connect(Line_ip,&QLineEdit::textChanged,[=](QString str){

           config->setValue(QString("network/ip"),str);


    });




    QPushButton *Increase_button=new QPushButton("增加",config_page);
    Buttoninit(Increase_button,"but_1","but_2");
    Increase_button->resize(button_W,button_H);
    Increase_button->show();
    Increase_button->move(init_page->width()-button_W-button_H/2,button_H*5);

    connect(Increase_button,&QPushButton::clicked,[=](){

        if(button_nums+1<18)
        {


            Line_vecors[button_nums-1]->setText(config->value(QString("button%1/name").arg(button_nums-1)).toString());
            But_vecors[button_nums-1]->setText(config->value(QString("button%1/name").arg(button_nums-1)).toString());

            button_nums++;
            But_vecors[button_nums-1]->show();
            Line_vecors[button_nums-1]->show();

            Line_vecors[button_nums-1]->setText("结束");
            But_vecors[button_nums-1]->setText("结束");



            config->setValue("prameter/button_num",button_nums);

        }
    });


    QPushButton * Decrease_button=new QPushButton("减少",config_page);
    Buttoninit(Decrease_button,"but_1","but_2");
    Decrease_button->resize(button_W,button_H);
    Decrease_button->show();
    Decrease_button->move(init_page->width()-button_W-button_H/2,button_H*7);

    connect( Decrease_button,&QPushButton::clicked,[=](){

        if(button_nums-1>0)
        {

            button_nums--;

            But_vecors[button_nums]->hide();
            Line_vecors[button_nums]->hide();

            Line_vecors[button_nums-1]->setText("结束");
            But_vecors[button_nums-1]->setText("结束");


            config->setValue("prameter/button_num",button_nums);

        }


    });
}




void MainWindow::Buttoninit(QPushButton *but, QString hover_img, QString pressed_img)
{
    QString Style = QString(
           "QPushButton{border-image: url(:data/%1.png);color:red;font-size:14px;}\
            QPushButton:hover {border-image: url(:data/%1.png);}\
            QPushButton:pressed {border-image: url(:data/%2.png);}").arg(hover_img).arg(pressed_img);
       but->setStyleSheet(Style);
}

